/**
 * Created by yaohuan on 2018/3/26 0026.
 */
define(function () {
    var self, pageUi,pjqsData;
    var list=[];
    function Bjqs() {
        self = this;
        self.mData = {
            tabType: ['1', '2'],
            orderSet: {}
        };
        self.searchData = {
            date: '',
            type: 0,
        };
        self.currentIndex = 0;
        self.init();
    }

    Bjqs.prototype.getData = function (goods) {
        app.ajax({
            xurl: "api/wx/pjqs/getPjqsList",
            // xtime: '1d',
            xdata: {
                page:goods.page+1,
                limit: goods.limit,
                _type: goods.type
            },
            cb: function (result) {
                if (result.ok) {
                    if (goods.page == 0) {
                        goods.data.removeAll();
                    }
                    var data = result.data;
                    data.data.forEach(function (v) {
                        v.checked = false;
                        if (v.STATUS == 1) {
                            v.MMRV005 = v.MMRU;
                        }else
                            v.MMRV005=(v.MMRV005==undefined?0:v.MMRV005);
                    });
                    //goods.data.pushArray(data.data);
                    var data_ = result.data.data;
                    goods.data.pushArray(data_.addMap({checked: false}));
                    var refresh = goods.refresh;
                    if (goods.page == 0) {
                        refresh.endPulldownToRefresh();
                        if (result.data.data.length == 0) {
                            app.showNoData(goods._tip, self.currentIndex == 0 ? '无未签收单...' : '无已签收单...');
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(goods._tip);
                            refresh.refresh(true);
                        }
                    }
                    goods.page += 1;
                    refresh.endPullupToRefresh(data.data.length < goods.limit);
                }
            }
        });
    };

    // 提供外部方法- 刷新页面
    Bjqs.prototype.refesh = function () {
        var _type = self.mData.tabType[self.currentIndex];
        var goods = self.mData.orderSet[_type];
        goods.page = 0;
        self.getData(goods);
    };

    Bjqs.prototype.init = function () {

        mui.ready(function () {

            // pageUi = {
            //     footdiv: $('#footdiv')
            // };

            Vue.component('bjqs-item', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array,
                },
                methods: {
                    showDetail: function (item) {
                        if (item.STATUS == '1') {
                            app.openPage({
                                src: "pjqs/pjqs-info.html",
                                argsKey: 'pjqsinfo_page',
                                args: {
                                    item: item,
                                    cb: function () {

                                    }
                                }
                            });
                        }

                    },
                    minus: function (item) {
                        if (item.MMRV005 > 1) {
                            item.MMRV005--;
                        }
                    },
                    plus: function (item) {
                        if (item.MMRU > item.MMRV005) {
                            item.MMRV005++;
                        }
                    }
                }
            });

            var refreshData = function () {

                var _type = self.mData.tabType[self.currentIndex];
                var goods = self.mData.orderSet[_type];
                if (!goods) {
                    goods = self.mData.orderSet[_type] = {
                        page: 1,
                        limit: 15,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        },
                        type: _type
                    };

                    pjqsData = new Vue({
                        el: doc.getElementById('scroll' + _type),
                        data: {
                            items: goods.data
                        }
                    });

                    new Vue({
                        el:doc.getElementById('footdiv'),
                        methods:{
                            open:function () {
                                var check = 0
                                for (var i = 0; i < pjqsData.items.length; i++) {
                                    if (pjqsData.items[i].checked == true) {
                                        check++;
                                    }
                                }
                                if (check == 0) {
                                    app.toast('请选择需要签收的配件');
                                    return;
                                }
                                list.splice(0,list.length);
                                var _type = self.mData.tabType[self.currentIndex];
                                var goods = self.mData.orderSet[_type];
                                for(var i = 0;i<goods.data.length;i++){
                                    if(goods.data[i].checked==true){
                                        list.push(goods.data[i]);
                                    }
                                }

                                app.openPage({
                                    src: "pjqs/pjqs-info.html",
                                    argsKey: 'pjqsinfo_page',
                                    args: {
                                        item: list,
                                    }
                                });

                            }
                        }
                    })

                    goods._tip = $('#scroll' + _type);
                    goods.refresh = mui('#scroll' + _type).pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self.getData(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(goods);
                            }
                        }
                    });
                }
            };

            refreshData();

            setTimeout(function () {

                doc.getElementById('slider').addEventListener('slide', function (e) {
                    self.currentIndex = e.detail.slideNumber;
                    refreshData();
                });
                $('#allCheck').click(function () {
                    var _type = self.mData.tabType[self.currentIndex];
                    var goods = self.mData.orderSet[_type];
                    if($('#allCheck').is(':checked')){
                        $('#scroll input[type=checkbox]').prop('checked', true);
                        for(var i=0;i<pjqsData.items.length;i++){
                            pjqsData.items[i].checked = true;
                        }
                    }else{
                        $('#scroll input[type=checkbox]').prop('checked', false);
                        for(var i=0;i<pjqsData.items.length;i++){
                            pjqsData.items[i].checked = false;
                        }
                    }
                });


            }, 200);

        });
    };

    return new Bjqs();
});